﻿function putDate(splitStr){
		var ev=arguments.callee.caller.arguments[0]||window.event;
		var obj= ev.srcElement|| ev.target;	
		sd=new SetDate(obj,splitStr);
		sd.show();
		obj.focus();
		JBF.addEvent(JBF.doc,"click",sd.closeWin);
		ev.cancelBubble=true;

}
function putDateById(id,splitStr){
	var dateObj=JBF.el(id);
	var ev=arguments.callee.caller.arguments[0]||window.event;
	sd=new SetDate(dateObj,splitStr);
	sd.show();
	dateObj.focus();
	JBF.addEvent(JBF.doc,"click",sd.closeWin);
	ev.cancelBubble=true;
}
function SetDate(dateObj,splitStr){
		this.dateObj=dateObj;
		this.maxYear=2030;
		this.minYear=1990;
		this.splitStr='';
		if(splitStr){
			this.splitStr=splitStr;
		}

		//this.objName=objName;
		if(typeof(this.dateObj.value)=="string"&&this.dateObj.value.length>0){
			this.date=this.dateObj.value;
			try{
				if(this.splitStr){
					this.dataArray=this.date.split(this.splitStr);
					this.year=Number(this.dataArray[0]);
					this.month=Number(this.dataArray[1]);
					this.day=Number(this.dataArray[2]);
				}else{
					this.year=Number(this.date.substring(0,4));
					this.month=Number(this.date.substring(4,6));
					this.day=Number(this.date.substring(6,8));
				}
				this.newDate=new Date(this.year,this.month-1,1);//生成当前月的第一天
			}catch(e){
				alert("日期格式不正确");
			}
		}else{
			this.date=new Date();
			this.year=this.date.getFullYear();
			this.month=this.date.getMonth()+1;
			this.day=this.date.getDate();
			this.newDate=new Date(this.year,this.month-1,1);
		}
}
SetDate.prototype={
	constructor:SetDate,
	initDate:function(){
		this.days=["日","一","二","三","四","五","六"];
	},
	/*
	*动态整合区间的select下拉框
	*/
	getYearsOrMonth:function(min,max,selValue){
		var selectStr="<select style='float:left;' >";
		for(var i=min;i<=max;i++){
			var selStr="";
			if(i==selValue){
				selStr=" selected=true ";
			}
			selectStr+="<option "+selStr+" value="+i+">"+i+"</option>";
		}
		selectStr+="</select>";
		return selectStr;
	},
	/*
	*
	*/
	show:function(){
		this.initDate();
		this.startWeek=this.newDate.getDay();//开始是周几
		this.datas=this.putDays();
		var x=JBF.getAbsX(this.dateObj);
		var y=JBF.getAbsY(this.dateObj)+this.dateObj.offsetHeight;
		var rows=0;
		if(this.datas.length%7==0){
			rows=parseInt(this.datas.length/7);
		}else{
			rows=parseInt(this.datas.length/7)+1;
		}
		var dateStr="<table cellspacing='1' cellpadding='1' bgcolor='#a3c0e8'><tbody>";
		dateStr+="<tr class='topPanel'><td colspan='7' height='20px'>";
		dateStr+="<div class='prevYear'><a href='#'></a></div><div class='prevMonth'><a href='#'></a></div>"+this.getYearsOrMonth(this.minYear,this.maxYear,this.year)+this.getYearsOrMonth(1,12,this.month)+"<div class='nextMonth'><a href='#'></a></div><div class='nextYear'><a href='#'></a></div>";
		dateStr+="</td></tr>";
		for(var rIndex=0;rIndex<rows;rIndex++){
			var tr="<tr class='trCss'>";
			for(var i=0;i<7;i++){
				var dataIndex=rIndex*7+i;
				var pojo=this.datas[dataIndex];
				var tdStr="<td  isReality='"+pojo+"'>";
				if(rIndex==0){
					tr+="<td style='text-align:center'>"+pojo+"</td>";
				}else{
					if(dataIndex>=this.datas.length){
						tr+=(tdStr+"&nbsp;</td>");
					}else{
						if(pojo==this.day){
							tdStr="<td class='tdOver' isReality='"+pojo+"'>";
						}else if((dataIndex)%7==0||(dataIndex+1)%7==0){
							tdStr="<td class='weekend' isReality='"+pojo+"'>";
						}
						tr+=(tdStr+pojo+"</td>");
					}
				}
			}
			dateStr+=(tr+"</tr>");
		}
		dateStr+="<tr><td colspan='7' align='center' height='16px'><div style='margin:auto;width:100%; '><div class='today'><a href='#' op='today'>今天</a></div><div class='today close'><a href='#'>清空</a></div></div></td></tr>";
		
		if(typeof(dateDivPanel)!="undefined"){
			this.closeWin();
		}
		dateDivPanel=document.createElement("div");
		dateDivPanel.style.position="absolute";
		dateDivPanel.style.left=x+"px";
		dateDivPanel.style.top=y+"px";
		document.body.appendChild(dateDivPanel);
		//=dateDiv;
		JBF.addEvent(dateDivPanel,"click",this.dateOnclick(this));
		JBF.addEvent(dateDivPanel,"mouseover",this.dateOnMouseOver());
		JBF.addEvent(dateDivPanel,"mouseout",this.dateOnMouseOut());
		//}

		dateDivPanel.innerHTML=dateStr+"</tbody></table>";
	},
	todayValue:function(){
		var nowDate=new Date();
		this.year=nowDate.getFullYear();
		this.month=nowDate.getMonth()+1;
		this.day=nowDate.getDate();
		this.dateObj.value=this.year+this.splitStr+this.makeUpZero(this.month+'',2)+this.splitStr+this.makeUpZero(this.day+'',2);
		this.closeWin();
	},
	closeWin:function(){
		if(typeof(dateDivPanel)!="undefined"){
			var parentNode=dateDivPanel.parentNode;
			if(parentNode){
				parentNode.removeChild(dateDivPanel);
				JBF.removeEvent(JBF.doc,"click",window.sd.closeWin);
			}
		}
	},
	makeUpZero:function(value,length){
		value+='';
		if(value.length<length){
			var makeUpLen=length-value.length;
			for(var i=makeUpLen;i>0;i--){
				value=("0"+value);	
			}	
		}
		return value;
	},
	/*
	*公共的单击事件，用来监听面板当中的所有操作
	*/
	dateOnclick:function(op){
		return function(event){
			var obj=JBF.getEventTarget(event);
			if(obj.nodeName=="TD"){
				var selValue=obj.innerText||obj.textContent;
				selValue=selValue.replace(" ","");
				if(obj.attributes["isReality"]&&selValue.length>0){
					op.day=selValue;
					op.dateObj.value=op.year+op.splitStr+op.makeUpZero(op.month,2)+op.splitStr+op.makeUpZero(op.day,2);
					op.closeWin();
				}
			}else if(obj.nodeName=="A"){
					var parentNode=obj.parentNode;
					if(parentNode.className){
						var opBit=false;
						switch (parentNode.className){
							case "prevYear":
								if(op.year<=op.minYear){
									op.year=op.maxYear;	
								}else{
									op.year=parseInt(op.year)-1;	
								}
								opBit=true;
								break;
							case "nextYear":
								if(op.year>=op.maxYear){
									op.year=op.minYear;
								}else{
									op.year=parseInt(op.year)+1;
								}
								opBit=true;
								break;
							case "prevMonth":
								if(op.month==1){
									if(op.year<=op.minYear){
										op.year=op.minYear;	
									}else{
										op.year-=1;
									}
									op.month=12;
								}else{
									op.month-=1;
								}
								opBit=true;
								break;
							case "nextMonth":
								if(op.month>=12){
									if(op.year>=op.maxYear){
										op.year=op.maxYear;	
									}else{
										op.year+=1;
									}
									op.month=1;
								}else{
									op.month+=1;
								}
								opBit=true;
								break;
							case "today":
								op.todayValue();
								break;
							case "today close":
								op.dateObj.value="";
								op.closeWin();
								break;
						}
						if(opBit){
							op.days.length=0;
							op.newDate=new Date(op.year,op.month-1,1);//生成当前月的第一天
							op.show();
						}

					}
				}else if(obj.nodeName=="SELECT"){
					JBF.addEvent(obj,"change",op.yearUpdate(op));
				}
			event.cancelBubble=true;
		};
	},
	/*
	*年份与日期下拉框更改时的事件
	*/
	yearUpdate:function(op){
		//var ev=arguments.callee.caller.arguments[0]||window.event;
		return function(event){
			var obj=JBF.getEventTarget(event);
			//alert(obj.nodeName);
			if(obj.nodeName=="SELECT"){
					var value=obj.options[obj.selectedIndex].value;
					if(value.length>2){
						op.year=value;
					}else{
						op.month=value;	
					}
					op.days.length=0;
					op.newDate=new Date(op.year,op.month-1,1);//生成当前月的第一天
					op.show();	
			}
		};
	},
	/*
	*鼠标经过事件
	*/
	dateOnMouseOver:function(){
		//var ev=arguments.callee.caller.arguments[0]||window.event;
		return function(event){
			var obj=JBF.getEventTarget(event);
			if(obj.nodeName=="TD"&&obj.attributes["isReality"]){
				obj.className+=" tdOver";
			}
		};
	},
	/*
	*鼠标移出事件
	*/
	dateOnMouseOut:function(){
		//var ev=arguments.callee.caller.arguments[0]||window.event;
		return function(event){
			var obj=JBF.getEventTarget(event);
			if(obj.nodeName=="TD"&&obj.attributes["isReality"]){
				if(!obj.attributes["selected"]){
					obj.className=obj.className.replace(" tdOver","");
				}
			}
		};
	},
	/*把所有数据全部压缩到表格当中
	*/
	putDays:function(){
		for(var i=0;i<this.startWeek;i++){
			this.days.push("&nbsp;");
		}
		monthDays=this.getMonthDays(this.month);
		for(var i=1;i<=monthDays;i++){
			this.days.push(i);
		}
		return this.days;
	},
	/*
	*通过当前年，及月份，得到当前月有几天
	*/
	getMonthDays:function(){
		var monthDay=30;
		switch(Number(this.month)){
			//case 1||3||5||7||8||10||12:
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				monthDay=31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				monthDay=30;
				break;
			case 2:
				if((this.year % 400 == 0)||(this.year % 4 == 0)&&(this.year % 100 != 0)){
					monthDay=29;
				}else{
					monthDay=28;
				}
				break;
			default:
				monthDay=30;
		}
		return monthDay;
	}
};
